from django.shortcuts import render

# Create your views here.
from rest_framework.generics import CreateAPIView,ListAPIView
from . import models
from rest_framework.views import APIView
from .serializers import OrderModelSerializer, OrderDataModelSerializer
from rest_framework.permissions import IsAuthenticated
from django.conf import settings
from rest_framework.response import Response

class OrderView(CreateAPIView):
    queryset = models.Order.objects.filter(is_show=True,is_deleted=False)
    serializer_class = OrderModelSerializer
    permission_classes = [IsAuthenticated, ]



class OrderDataView(ListAPIView):
    permission_classes = [IsAuthenticated, ]
    serializer_class = OrderDataModelSerializer
    # queryset = models.Order.objects.filter(is_show=True,is_deleted=False,)
    # 获取所有订单
    def get_queryset(self):
        queryset = models.Order.objects.filter(is_deleted=False, user_id=self.request.user.id)

        return queryset


from lyapi.libs.polyv.myp import PolyvPlayer

class VideoView(APIView):
    permission_classes = [IsAuthenticated, ]
    def get(self,request):
        polyv_obj = PolyvPlayer(
            settings.POLYV_SETTINGS.get('userId'),
            settings.POLYV_SETTINGS.get('secretkey'),
            settings.POLYV_SETTINGS.get('tokenUrl'),
        )
        # videoId, viewerIp, viewerId=None, viewerName=''
        vid = request.query_params.get('vid')
        # videoId = '869c62ab0c197eb262169916eef359f3_8'
        viewerIp = request.META.get('REMOTE_ADDR')
        viewerId = request.user.id
        viewerName = request.user.username

        token_data = polyv_obj.get_video_token(
            vid,
            viewerIp,
            viewerId,
            viewerName,
        )
        token = token_data.get('token')


        return Response({'token_data': token})


